<script>
export default {
  data() {
    return {
      isShow: false
    }
  },
  methods: {
    showInput() {
      this.isShow = true
    },
    updateTag(e) {
      if (e.target.value.trim() === '') {
        return alert('输入内容不能为空')
      }
      this.$emit('input', e.target.value)
      this.isShow = false
    }
  },
  props: {
    value: String
  }
}
</script>

<template>
  <div class="my-tag">
    <input
        :value="value"
        class="input"
        type="text"
        placeholder="输入标签"
        v-if="isShow"
        v-focus
        @blur="isShow=false"
        @keyup.enter="updateTag"
    />
    <div class="text" v-else @dblclick="showInput">
      {{value}}
    </div>
  </div>
</template>

<style lang="less" scoped>
.my-tag {
  cursor: pointer;

  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;

    &::placeholder {
      color: #666;
    }
  }
}
</style>